import React, { useState } from 'react';
import { Input, TreeSelect, Tree } from 'antd';
import union from 'lodash/union';
import difference from 'lodash/difference';
import { treeToLine, getParentKey, getParentList, getChildrenList } from '../utils';
import styles from '../style.less';

const { TreeNode } = Tree;
const { Search } = Input;

/**
 * 角色所需功能 搜索树
 * @param list menuTree
 */
const SearchTree = React.forwardRef((props, ref) => {
    const { list: gData, onCheck, valueProps, isPortAuth, ...otherProps } = props;
    const [expandedKeys, setExpandedKeys] = useState([]);
    const [searchValue, setSearchValue] = useState('');
    const [autoExpandParent, setAutoExpandParent] = useState(true);
    /**
     * 搜索框onChange 事件
     * @param {*} e
     */
    const onChange = e => {
        const { value } = e.target;
        const newExpandedKeys = treeToLine(gData)
            .map(item => {
                if (item.title.indexOf(value) > -1) {
                    return getParentKey(item.key, gData);
                }
                return null;
            })
            .filter((item, i, self) => item && self.indexOf(item) === i);
        setExpandedKeys(newExpandedKeys);
        setSearchValue(value);
        setAutoExpandParent(true);
    };
    const onExpand = expandedKey => {
        setExpandedKeys(expandedKey);
        setAutoExpandParent(false);
    };
    /**
     * Tree 点击复选框触发
     * 1、选择父菜单，子菜单不要自动勾选；
     * 2、选择子菜单，父菜单要自动勾选
     * 3、取消父菜单，子菜单要自动取消勾选；
     * 4、取消子菜单，父菜单不要自动取消勾选；
     * @param {*} checkedKeys
     * @param {*} e
     */
    const handelCheck = (checkedKeys, e) => {
        const childList = [];
        let CheckedKeysTmp = checkedKeys.checked;
        if (e.checked) {
            if (e.node.props.vvItem.parentId) {
                CheckedKeysTmp = union([...checkedKeys.checked, ...getParentList(e.node.props.eventKey, gData)]);
            }
        } else {
            getChildrenList(e.node.props.vvItem, childList);
            CheckedKeysTmp = difference(checkedKeys.checked, childList);
        }
        onCheck(CheckedKeysTmp, e);
    };
    const loop = (data = []) =>
        data.map(item => {
            const index = item.title.indexOf(searchValue);
            const beforeStr = item.title.substr(0, index);
            const afterStr = item.title.substr(index + searchValue.length);
            const title =
                index > -1 ? (
                    <span>
                        {beforeStr}
                        <span style={{ color: '#f50' }}>{searchValue}</span>
                        {afterStr}
                    </span>
                ) : (
                    <span>{item.title}</span>
                );
            if (item.children) {
                return (
                    <TreeNode key={item.key} title={title} vvItem={item}>
                        {loop(item.children)}
                    </TreeNode>
                );
            }
            return <TreeNode key={item.key} title={title} />;
        });
    return (
        <div ref={ref}>
            <Search
                {...otherProps}
                style={{ marginBottom: 8 }}
                className={isPortAuth ? styles.notify : ''}
                placeholder="请输入菜单名称"
                onChange={onChange}
                onPressEnter={e => {
                    e.preventDefault();
                }}
            />
            <Tree
                {...otherProps}
                checkable
                // treeCheckStrictly
                checkedKeys={valueProps}
                checkStrictly
                onCheck={handelCheck}
                showCheckedStrategy={TreeSelect.SHOW_ALL}
                onExpand={onExpand}
                expandedKeys={expandedKeys}
                autoExpandParent={autoExpandParent}
            >
                {loop(gData)}
            </Tree>
        </div>
    );
});
export default SearchTree;
